// pages/product/SpellGroupMore.js
var Zan = require('../../dist/index.js');
let network = null;
let utils = null;
let api = null;
let app = getApp()
var start = 0;
function countdown(that) {
  for (var i = 0; i < that.data.tuanzhang_list.length; i++) {
    var EndTime = that.data.tuanzhang_list[i].endDate || [];
    var NowTime = new Date().getTime();
    var total_micro_second = EndTime - NowTime || [];
    // 渲染倒计时时钟
    that.data.tuanzhang_list[i].clock = dateformat(total_micro_second)
    // that.setData({
    //   clock: dateformat(total_micro_second)
    // });
    if (total_micro_second <= 0) {
      that.data.tuanzhang_list[i].clock = "已经截止"
      //return;
    }
  }
  that.setData({
    tuanzhang_list: that.data.tuanzhang_list
  })

  setTimeout(function () {
    total_micro_second -= 1000;
    countdown(that);
  }
    , 1000)
}

// 时间格式化输出，如11:03 25:19 每1s都会调用一次
function dateformat(micro_second) {
  // 总秒数
  var second = Math.floor(micro_second / 1000);
  // 天数
  var day = Math.floor(second / 3600 / 24);
  // 小时
  var hr = Math.floor(second / 3600 % 24);
  // 分钟
  var min = Math.floor(second / 60 % 60);
  // 秒
  var sec = Math.floor(second % 60);
  return day + ":" + hr + ":" + min + ":" + sec;
}
Page(Object.assign({}, Zan.Tab, Zan.TopTips,{

  /**
   * 页面的初始数据
   */
  data: {
    tuanzhang_list:[],
    spellGroupId:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    network = new app.NetWorkUtils();
    utils = new app.Utils();
    api = new app.Api();

    this.setData({
      spellGroupId: options.spellGroupsId
    })

    network._post_json(this, api.getSpellGroupsDetail, { spellGroupsId: this.data.spellGroupId }, success );

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    network._post_json(this, api.groupList, {}, success);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {
  
  // },
  gotoCantuan:function(e){
    var tuanitem = e.currentTarget.dataset.tuanitem;
    this.setData({
      spellGroupsId: tuanitem.spellGroupsId,
      sgCombId: tuanitem.sgCombId,
      nowclock: tuanitem.clock,
      userSpellGroupId: tuanitem.id,
      originator: tuanitem.originator
    })
    network._post_json(this, api.spellGroupsActiveDetail, { sgCombId: this.data.sgCombId, spellGroupsId: this.data.spellGroupId, status: 0, originator: this.data.originator }, FSuccess);
  }
}))

var success = function (that, res){
  if (res.data.data){
    that.setData({
      tuanzhang_list: res.data.data.data
    })
  }
  countdown(that);
}

var FSuccess = function (that, res) {
  if (res.data.data.specType1 && !res.data.data.specType2 && !res.data.data.specType2) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1
  }
  if (res.data.data.specType2) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1 + '   ' + res.data.data.specType2 + ':' + res.data.data.spellGroupsComb.specDetails2
  }
  if (res.data.data.specType3) {
    res.data.data.spellGroupsComb.specDetails = res.data.data.specType1 + ':' + res.data.data.spellGroupsComb.specDetails1 + '   ' + res.data.data.specType2 + ':' + res.data.data.spellGroupsComb.specDetails2 + '   ' + res.data.data.specType3 + ':' + res.data.data.spellGroupsComb.specDetails3
  }

  if (res.data.data.spellGroupsDetails && res.data.data.spellGroupsDetails.length > 0) {
    for (var i = 0; i < res.data.data.spellGroupsDetails.length; i++) {
      if (res.data.data.spellGroupsDetails[i].originator == res.data.data.spellGroupsDetails[i].userId) {
        //获取团长购买的数量
        res.data.data.count = res.data.data.spellGroupsDetails[i].orderProductDetail.count;
        res.data.data.pintuanprice = res.data.data.spellGroupsDetails[i].orderProductDetail.price2
        res.data.data.danmaiprice = res.data.data.spellGroupsDetails[i].orderProductDetail.price
        break;
      }
    }
  }
  if (res.data.data) {
    that.setData({
      groupsellDetail: res.data.data,
      specDetails: res.data.data.spellGroupsComb ? res.data.data.spellGroupsComb.specDetails : '',
      cantuanlist: res.data.data.spellGroupsDetails
    })

  }
  
  if (that.data.nowclock != "已经截止") {
    wx.navigateTo({
      url: '/pages/product/SpellGroupDetails?spellGroupId=' + that.data.spellGroupId + '&userSpellGroupId=' + that.data.userSpellGroupId + '&sgCombId=' + that.data.sgCombId + '&specName=' + that.data.specDetails + '&orderprice=' + that.data.groupsellDetail.pintuanprice + '&proprice=' + that.data.groupsellDetail.danmaiprice + '&count=' + that.data.groupsellDetail.count,
    })
  }


}